
// #include "xsdk_config.h"

#include "xapp_config.h"

#if USER_CHECK_ENABLED( TOUCHPANEL )

#include <string.h>

#include "xapp_touchpanel.h"

#include "em_touch_panel.h"

#include "xtask+app.h"
#include "xtask+peripheral.h"
#include "xapp_profile.h"

#include "pbif.h"

#include "gui.h"
#include "touch_algorithm.h"
#if USER_CHECK_LOG_ENABLED( TOUCHPANEL )
#include "log.h"
#define LOG     LOG_INFO 
#define LOG0    LOG_INFO0 
#define LOG1    LOG_INFO1
#define LOG2    LOG_INFO2
#define LOG3    LOG_INFO3
#else 
#define LOG(...) 
#define LOG0(...)
#define LOG1(...)
#define LOG2(...)
#define LOG3(...)
#endif 

static xapp_touchpanel_desc_t tp_desc = {.mounted = false};

static void touch_panel_callback_handler( em_touch_panel_evt_t* p_evt )
{
    switch( p_evt->type )
    {
        case EM_TOUCH_PANEL_EVT_TYPE_INT:
        {
            msg_t msg;
            msg.type = MSG_TYPE_TOUCHPANEL;
            msg.sub_type = MSG_TOUCHPANEL_DATA_UPDATE;
            xtask_peripheral_send_msg( &msg );
        }
        break;

        case EM_TOUCH_PANEL_EVT_TYPE_TIMEOUT:
        {
            msg_t msg;
            msg.type = MSG_TYPE_TOUCHPANEL;
            msg.sub_type = MSG_TOUCHPANEL_PROCESS;
            xtask_peripheral_send_msg( &msg );
        }
        break;
    }
}

void xapp_touchpanel_init(void)
{
    memset( &tp_desc, 0, sizeof(tp_desc));
    
    em_touch_panel_register_callback( touch_panel_callback_handler ); 

    touch_algorithm_callback_register( (tp_algo_evt_handle_t)gui_on_touch_event );
    
    em_touch_panel_init();
}

const xapp_touchpanel_desc_t* xapp_touch_panel_get_desc(void)
{
    return &tp_desc;
}

void xapp_touchpanel_mounting(void)
{      
    em_touch_panel_desc_t desc;
    desc.chip_id = 0xff;
    desc.version = 0xff;
    
    tp_desc.mounted = em_touch_panel_mounting( &desc.chip_id, &desc.version );
    tp_desc.chip_id = desc.chip_id;
    tp_desc.version = desc.version;
    
    if( tp_desc.mounted ) 
    {
        tp_desc.stage = XAPP_TOUCHPANEL_STAGE_WORK; 
        LOG0("[TP]: mounted succ");
    }
    else 
    {
        LOG0("[TP]: mounted fail");
    }
}

void xapp_touchpanel_setup(void)
{
    msg_t msg;
    msg.type = MSG_TYPE_TOUCHPANEL;
    msg.sub_type = MSG_TOUCHPANEL_SETUP;
    xtask_peripheral_send_msg( &msg );    
}

void xapp_touchpanel_start_upgrade(void)
{
    tp_desc.stage = XAPP_TOUCHPANEL_STAGE_UPGRAGE;
    
    msg_t msg;
    msg.type = MSG_TYPE_TOUCHPANEL;
    msg.sub_type = MSG_TOUCHPANEL_BOOT;
    xtask_peripheral_send_msg( &msg );
//    xtask_app_send_msg( &msg, __LINE__ );
}

#if TOUCHPANEL_IS_UPGRADE_SUPPORTED
void xapp_touchpanel_upgrading(void)
{
    LOG0("[TP]: start upgrade");
    tp_desc.stage = XAPP_TOUCHPANEL_STAGE_UPGRAGE;
    
    em_touch_panel_boot_update( EM_TP_FW_ADDR_INTERNAL, &tp_desc.dfu_percent );
    
    tp_desc.stage = XAPP_TOUCHPANEL_STAGE_INIT;    
    xapp_touchpanel_mounting();
}
#endif 

// udpate touchpanel fifo & process data 
void xapp_touchpanel_data_update(void)
{
    em_touch_panel_update();
    
    xapp_touchpanel_data_process(); 
}

void xapp_touchpanel_set_notify(bool enable)
{
    tp_desc.notify = enable;
}

void xapp_touchpanel_data_process(void)
{
    uint8_t data[6*20];
    int index = 0;
    uint8_t cnt = 0;
    touch_raw_data_t const *p;
    do//while(1)
    {
        p = em_touch_panel_dequeue();
        if( p!= NULL )
        {
            touch_algorithm_process( (touch_raw_data_t*)p ); 
//            LOG3("[TOUCH]: f=%d, %d,%d\r\n", p->flag, p->x, p->y );
            if( tp_desc.notify )
            {
                if( (index + 6) < 60 )
                {
                    data[0 + index] = p->flag;
                    data[1 + index] = p->x >> 0;
                    data[2 + index] = p->x >> 8;
                    data[3 + index] = p->y >> 0;
                    data[4 + index] = p->y >> 8;
                    data[5 + index] = cnt;
                    index += 6; 
                }
            }
        }        
    }while( p != NULL );

    if( tp_desc.notify && index > 0 )
    {
        pi_mem_tp_data_notify( data, index );
    }
}

void xapp_touchpanel_enable(void)
{
    msg_t msg;
    msg.type = MSG_TYPE_TOUCHPANEL;
    msg.sub_type = MSG_TOUCHPANEL_ENABLE;
    xtask_peripheral_send_msg( &msg ); 
	 LOG0("tp enable \r\n");
}

void xapp_touchpanel_disable(void)
{
    msg_t msg;
    msg.type = MSG_TYPE_TOUCHPANEL;
    msg.sub_type = MSG_TOUCHPANEL_DISABLE;
    xtask_peripheral_send_msg( &msg );
}

void xapp_touchpanel_msg_handler(msg_t* p_msg)
{  
    switch( p_msg->sub_type )
    {
        case MSG_TOUCHPANEL_DISABLE:
            if( tp_desc.mounted )
            {
                em_touchpanel_disable();
				
				xapp_profile_store();//  BOARD_S7
				
            }
            break;
        case MSG_TOUCHPANEL_ENABLE:
            if( tp_desc.mounted )
            {
                em_touchpanel_enable();
                LOG0("touchpanel enable\r\n");
            }
            break;
        
        case MSG_TOUCHPANEL_DATA_UPDATE:
            xapp_touchpanel_data_update();
            break;
        
        case MSG_TOUCHPANEL_PROCESS:
            xapp_touchpanel_data_process();
            break;
        
        case MSG_TOUCHPANEL_SETUP:
            xapp_touchpanel_mounting();
            break;
        
        case MSG_TOUCHPANEL_BOOT:
#if TOUCHPANEL_IS_UPGRADE_SUPPORTED
            xapp_touchpanel_upgrading();
#endif
            break;
        default:
            break;
    }
}

#endif // !USER_CHECK_ENABLED( TOUCHPANEL )
